from flask import Blueprint, request, render_template
from model import House


def deal_title_over(word):
    if len(word) > 15:
        return word[:15] + '...'
    else:
        return word

def deal_direction(word):
    if len(word) == 0 or word is None:
        return '暂无信息'
    else:
        return word

# 创建蓝图对象
list_page = Blueprint('list_page', __name__)
list_page.add_app_template_filter(deal_title_over, 'dealover')
list_page.add_app_template_filter(deal_direction, 'deal_direction')
@list_page.route('/query')
def search_txt_info():
    # 获取 addr 字段的查询结果
    if request.args.get('addr'):
        addr = request.args.get('addr')
        result = House.query.filter(House.address == addr).order_by(House.publish_time.desc()).all()
        return render_template('search_list.html', house_list=result)

    # 获取 rooms 字段的查询结果
    if request.args.get('rooms'):
        rooms_info = request.args.get('rooms')
        result = House.query.filter(House.rooms == rooms_info).order_by(House.publish_time.desc()).all()
        return render_template('search_list.html', house_list=result)

# 如果没有提供任何查询参数，返回空列表
import math
@list_page.route('/list/pattern/<int:page>')
def return_list(page):
    house_num = House.query.count()
    total_num = math.ceil(house_num / 10)
    result = House.query.order_by(House.publish_time.desc()).paginate(page=page, per_page=10)
    return render_template('list.html', house_list=result.items, page_num=result.page, total_num=total_num)

